﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThreeByte.ActiveDeck.Config;
using System.Windows.Forms.Integration;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using ThreeByte.ActiveDeck.AddIn.Config;

namespace ThreeByte.ActiveDeck.Controls
{
    public partial class OperatorMonitorForm : Form
    {

        private OperatorMonitorControl _operatorControl;

        public OperatorMonitorForm(PresentationMonitor presentationMonitor) {
            InitializeComponent();
            
            _operatorControl = new OperatorMonitorControl(presentationMonitor);
            presentationMonitor.Disposed += new EventHandler(presentationMonitor_Disposed);

            //Add Setup Form
            ElementHost elementHost = new ElementHost();
            elementHost.Dock = DockStyle.Fill;
            elementHost.Location = new Point(0, 0);
            elementHost.Name = "elementHost";
            elementHost.Child = _operatorControl;
            elementHost.AutoSize = true;
            this.Controls.Add(elementHost);

            if (ThreeByte.ActiveDeck.AddIn.Properties.Settings.Default.PresentationMonitorSize != default(Size)) {
                this.Size = ThreeByte.ActiveDeck.AddIn.Properties.Settings.Default.PresentationMonitorSize;
            }
            if(ThreeByte.ActiveDeck.AddIn.Properties.Settings.Default.PresentationMonitorPosition != default(Point)) {
                StartPosition = FormStartPosition.Manual;
                Point pos = ThreeByte.ActiveDeck.AddIn.Properties.Settings.Default.PresentationMonitorPosition;
                this.Left = pos.X;
                this.Top = pos.Y;
            }

            this.Move += new EventHandler(OperatorMonitorForm_Move);
            this.SizeChanged += new EventHandler(OperatorMonitorForm_SizeChanged);
        }

        protected override bool ShowWithoutActivation {
            get {
                return true;
            }
        }

        void presentationMonitor_Disposed(object sender, EventArgs e) {
            this.Close();
        }

        private void OperatorMonitorForm_Move(object sender, EventArgs e) {
            StoreCurrentSizeAndPosition();
        }

        private void OperatorMonitorForm_SizeChanged(object sender, EventArgs e) {
            StoreCurrentSizeAndPosition();
        }

        private void StoreCurrentSizeAndPosition() {

            ThreeByte.ActiveDeck.AddIn.Properties.Settings.Default.PresentationMonitorSize = this.Size;
            ThreeByte.ActiveDeck.AddIn.Properties.Settings.Default.PresentationMonitorPosition = new Point(Left, Top);
            ThreeByte.ActiveDeck.AddIn.Properties.Settings.Default.Save();
            
        }

        public static void ResetSizeAndPosition() {
            ThreeByte.ActiveDeck.AddIn.Properties.Settings.Default.PresentationMonitorSize = new Size(500, 320);
            ThreeByte.ActiveDeck.AddIn.Properties.Settings.Default.PresentationMonitorPosition = new Point(0, 0);
            ThreeByte.ActiveDeck.AddIn.Properties.Settings.Default.Save();

        }
    }
}
